package com.cgbl.cg.ui.home

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import com.cgbl.cg.BR
import com.cgbl.cg.R
import com.cgbl.cg.adapter.MoreCourseAdapter
import com.cgbl.cg.adapter.MoreRecommendAdapter
import com.cgbl.cg.adapter.PopularMaterialAdapter
import com.cgbl.cg.adapter.RecommendMasterCourseAdapter
import com.cgbl.cg.databinding.AppFragmentRecommendMasterCourseBinding
import com.cgbl.cg.vm.home.RecommendMasterCourseFragmentViewModel
import com.cgbl.cg.vm.home.RecommendMasterCourseViewModel
import com.robot.base.base.activity.GLBaseFragment
import com.robot.base.configs.AppConstants
import com.robot.base.model.CourseBean
import com.robot.base.model.ProjectBean
import com.robot.base.model.ResourceBean

class RecommendMasterCourseFragment :
    GLBaseFragment<AppFragmentRecommendMasterCourseBinding, RecommendMasterCourseFragmentViewModel>() {
    override fun initContentView(
        inflater: LayoutInflater?,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): Int = R.layout.app_fragment_recommend_master_course

    override fun initVariableId(): Int = BR.viewModel

    private lateinit var recommendMasterCourseAdapter: RecommendMasterCourseAdapter
    private lateinit var moreRecommendAdapter: MoreRecommendAdapter
    private lateinit var moreResourceAdapter: PopularMaterialAdapter
    private lateinit var moreCourseAdapter: MoreCourseAdapter


    var id = ""
    var type = AppConstants.BundleValue.MASTER_COURSE
    var tabType = RecommendMasterCourseViewModel.TAB_COURSE

    companion object {
        fun newInstance(id: String, type: Int, tabType: Int): RecommendMasterCourseFragment {
            val fragment = RecommendMasterCourseFragment()
            val bundle = Bundle()
            bundle.putInt("type", type)
            bundle.putString("id", id)
            bundle.putInt("tabType", tabType)
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun initParam() {
        super.initParam()
        type = arguments?.getInt("type") ?: AppConstants.BundleValue.MASTER_COURSE
        id = arguments?.getString("id") ?: ""
        tabType = arguments?.getInt("tabType") ?: RecommendMasterCourseViewModel.TAB_COURSE
    }

    override fun initComponents() {
        initAdapter()
        viewModel.type.set(type)
        viewModel.tabType.set(tabType)
        viewModel.id = id
        viewModel.refresh()
    }

    fun initAdapter() {
        recommendMasterCourseAdapter = RecommendMasterCourseAdapter()
        moreRecommendAdapter = MoreRecommendAdapter()
        moreResourceAdapter = PopularMaterialAdapter()
        moreCourseAdapter = MoreCourseAdapter(false)
        binding.recyclerView.apply {
            when (type) {
                AppConstants.BundleValue.MASTER_COURSE -> {
                    adapter = recommendMasterCourseAdapter
                }

                AppConstants.BundleValue.CLASS_CASE -> {
                    adapter = moreRecommendAdapter
                }

                AppConstants.BundleValue.POPULAR_RESOURCES -> {
                    adapter = moreResourceAdapter
                }

                AppConstants.BundleValue.VIP -> {
                    adapter = moreCourseAdapter
                }
            }
        }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        binding.smartRefreshLayout.setOnRefreshListener {
            viewModel.refresh()
        }

        binding.smartRefreshLayout.setOnLoadMoreListener {
            viewModel.load()
        }

        viewModel.uc.projectEvent.observe(this){
            if (viewModel.page == 1) {
                moreRecommendAdapter.setNewInstance(it as MutableList<ProjectBean>?)
            }else{
                moreRecommendAdapter.addData(it)
            }

        }
        viewModel.uc.refreshEvent.observe(this) {
            binding.smartRefreshLayout.finishRefresh()
            binding.smartRefreshLayout.finishLoadMore()
            binding.smartRefreshLayout.setEnableLoadMore(it != 1)
        }

        viewModel.uc.resourceEvent.observe(this) {
            it ?: return@observe
            if (viewModel.page == 1) {
                when (type) {
                    AppConstants.BundleValue.POPULAR_RESOURCES -> {
                        moreResourceAdapter.setNewInstance(it as MutableList<ResourceBean>?)
                    }

                    AppConstants.BundleValue.VIP -> {
                        moreResourceAdapter.setNewInstance(it as MutableList<ResourceBean>?)

                    }
                }
            } else {
                when (type) {
                    AppConstants.BundleValue.POPULAR_RESOURCES -> {
                        moreResourceAdapter.addData(it)
                    }

                    AppConstants.BundleValue.VIP -> {
                        moreResourceAdapter.addData(it)
                    }
                }
            }
        }

        viewModel.uc.courseEvent.observe(this) {
            it ?: return@observe
            if (viewModel.page == 1) {
                when (type) {
                    AppConstants.BundleValue.MASTER_COURSE -> {
                        recommendMasterCourseAdapter.setNewInstance(it as MutableList<CourseBean>?)
                    }


                    AppConstants.BundleValue.VIP -> {
                        moreCourseAdapter.setNewInstance(it as MutableList<CourseBean>?)
                    }
                }
            } else {
                when (type) {
                    AppConstants.BundleValue.MASTER_COURSE -> {
                        recommendMasterCourseAdapter.addData(it)
                    }

                    AppConstants.BundleValue.VIP -> {
                        moreCourseAdapter.addData(it)
                    }
                }
            }
        }
    }
}